package xyz.jangle.design.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ConcretePrototype2 implements Prototype, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1905246759025489290L;
	
	private String str;

	public ConcretePrototype2(String str) {
		super();
		this.str = str;
	}

	@Override
	public Object cloneSelf() throws CloneNotSupportedException {
		Object object = null;
		try {
			ByteArrayOutputStream out1 = new ByteArrayOutputStream();
			ObjectOutputStream out2 = new ObjectOutputStream(out1);
			out2.writeObject(this);
			ByteArrayInputStream in1 = new ByteArrayInputStream(out1.toByteArray());
			ObjectInputStream in2 = new ObjectInputStream(in1);
			object = in2.readObject();
			in2.close();
			out2.close();//对于ObjectOutputStream，关闭它也会关闭其底层的ByteArrayOutputStream，
						 //因为ObjectOutputStream封装了ByteArrayOutputStream。所以，你只需要关闭ObjectOutputStream即可
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
	
	

}
